package wang.z.JFrame;
import wang.z.component.AdminBookAdd;
import wang.z.component.BackGroundPanel;
import wang.z.model.ResultInfo;
import wang.z.net.FailListener;
import wang.z.net.PostUtils;
import wang.z.net.SuccessListener;
import wang.z.utils.JsonUtils;
import wang.z.utils.PathUtils;
import wang.z.utils.ScreenUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author like
 * @date 2022/12/3 23:44
 * @Description TODO
 */
public class loginFrm {
    final int WIDTH = 600;
    final int HEIGHT = 500;
    private JFrame jf = new JFrame();

    public void init() throws IOException {

        jf.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2, (ScreenUtils.getScreenHeight() - HEIGHT) / 2,500,300);


        //组装登录相关的元素
        Box vBox = Box.createVerticalBox();
        BackGroundPanel bgPanel = new BackGroundPanel(ImageIO.read(new File(PathUtils.getRealPath("library.jpg"))));
        bgPanel.setBounds(500,300,700,500);
        //组装用户名
        Box uBox = Box.createHorizontalBox();
        JLabel uLabel = new JLabel("用户名：");
        uLabel.setFont(new Font("幼圆",Font.BOLD, 16));
        uLabel.setForeground(Color.BLACK);
        JTextField uField = new JTextField(15);
        uBox.add(uLabel);
        uBox.add(Box.createHorizontalStrut(20));
        uBox.add(uField);

        //组装密码
        Box pBox = Box.createHorizontalBox();
        JLabel pLabel = new JLabel("密  码：");
        JPasswordField  pField = new JPasswordField (15);
        pLabel.setFont(new Font("幼圆",Font.BOLD, 16));
        pLabel.setForeground(Color.BLACK);
        pBox.add(pLabel);
        pBox.add(Box.createHorizontalStrut(19));
        pBox.add(pField);

        //组装按钮
        Box btnBox = Box.createHorizontalBox();
        JButton loginBtn = new JButton("登录");
        JButton registBtn = new JButton("注册");
        btnBox.add(loginBtn);
        btnBox.add(Box.createHorizontalStrut(100));
        btnBox.add(registBtn);
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取用户输入的数据
                String username = uField.getText().trim();
                String password = pField.getText().trim();
                Map<String,String> params = new HashMap<>();
                params.put("username",username);
                params.put("password",password);

                //访问登录接口
                PostUtils.postWithParams("http://localhost:8080/user/login", params, new SuccessListener() {
                    //请求发送成功，服务器正常响应，会执行这里
                    @Override
                    public void success(String result) {//result参数就是服务器响应回来的json字符串
                        ResultInfo info = JsonUtils.parseResult(result);
                        //判断info中的flag标记
                        if (info.isFlag()){
                            //登录成功,跳转到主页面
                            try {

                                if((int)info.getData() == 1){
                                    new UserFace().init();
                                }else {
                                    new Manager().init();
                                }

                                jf.dispose();
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }else{
                            // 登录失败
                            JOptionPane.showMessageDialog(jf,info.getMessage());
                        }
                    }
                }, new FailListener() {
                    //当该请求发送失败后会执行这个方法
                    @Override
                    public void fail() {
                        JOptionPane.showMessageDialog(jf,"网络异常，请稍后重试");
                    }
                });

            }
        });

        registBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //跳转到注册页面
                try {
                    new last().init();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                //当前界面消失
                jf.dispose();
            }
        });
        vBox.add(Box.createVerticalStrut(50));
        vBox.add(uBox);
        vBox.add(Box.createVerticalStrut(20));
        vBox.add(pBox);
        vBox.add(Box.createVerticalStrut(40));
        vBox.add(btnBox);

        bgPanel.add(vBox);
        jf.add(bgPanel);



        jf.setVisible(true);
    }
    public static void main(String[] args) throws IOException {

            new loginFrm().init();

    }

}
